package com.levi.test.proxy;

import com.alibaba.fastjson.JSONObject;
import com.levi.test.bean.ExecuteRequest;
import com.levi.test.util.ExecuteHttpUtil;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ExecuteProxy implements MethodInterceptor {

    @Override
    public Object intercept(Object object, Method method, Object[] objects, MethodProxy proxy) {
        try {
            ExecuteRequest req = new ExecuteRequest();
            req.setC(Class.forName(object.getClass().getName().split("\\$\\$")[0]));
            req.setM(method.getName());
            req.setO(objects);
            req.setT(method.getParameterTypes());
            if ("toString".equals(req.getM())) {
                return null;
            }
            if (req.getC() == null || req.getM() == null) {
                throw new RuntimeException("test入参异常：" + JSONObject.toJSONString(req));
            }
            ExecuteHttpUtil.http(JSONObject.toJSONString(req));
            return null;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }
}
